package com.nine.gxh.config;

import com.ruoyi.common.constant.MyRabbitDelayConstants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: duonine
 * @date: 2023/7/7
 */
@Configuration
public class MyDeadLetterRabbitConfig {

    // 申明延时Exchange
    @Bean("delayExchange")
    public DirectExchange delayExchange() {
        return new DirectExchange(MyRabbitDelayConstants.DELAY_EXCHANGE_NAME);
    }

    // 申明死信Exchange
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(MyRabbitDelayConstants.DEAD_LETTER_EXCHANGE_NAME);
    }

    // 申明延时队列A，并并绑定到对应的死信交换机
    @Bean("delayQueueA")
    public Queue delayQueueA() {
        Map<String, Object> args = new HashMap<>(2);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", MyRabbitDelayConstants.DEAD_LETTER_EXCHANGE_NAME);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", MyRabbitDelayConstants.DEAD_LETTER_ROUTE_NAME_10);
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl", 1000 * 10);
        return QueueBuilder.durable(MyRabbitDelayConstants.DELAY_QUEUE_NAME_A).withArguments(args).build();
    }

    // 声明延时队列B 延时 30s，并绑定到对应的死信交换机
    @Bean("delayQueueB")
    public Queue delayQueueB() {
        Map<String, Object> args = new HashMap<>(2);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", MyRabbitDelayConstants.DEAD_LETTER_EXCHANGE_NAME);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", MyRabbitDelayConstants.DEAD_LETTER_ROUTE_NAME_30);
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl", 1000 * 30);
        return QueueBuilder.durable(MyRabbitDelayConstants.DELAY_QUEUE_NAME_B).withArguments(args).build();
    }

    // 申明死信队列A
    @Bean("deadLetterQueueA")
    public Queue deadLetterQueueA() {
        return new Queue(MyRabbitDelayConstants.DEAD_LETTER_QUEUE_NAME_A);
    }

    // 申明死信队列B
    @Bean("deadLetterQueueB")
    public Queue deadLetterQueueB() {
        return new Queue(MyRabbitDelayConstants.DEAD_LETTER_QUEUE_NAME_B);
    }

    // 声明延时队列A绑定关系
    @Bean
    public Binding delayBindingA(@Qualifier("delayQueueA") Queue queue, @Qualifier("delayExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(MyRabbitDelayConstants.DELAY_ROUTE_NAME_1);
    }

    // 声明业务队列B绑定关系
    @Bean
    public Binding delayBindingB(@Qualifier("delayQueueB") Queue queue, @Qualifier("delayExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(MyRabbitDelayConstants.DELAY_ROUTE_NAME_2);
    }

    // 声明死信队列A绑定关系
    @Bean
    public Binding deadLetterBindingA(@Qualifier("deadLetterQueueA") Queue queue, @Qualifier("deadLetterExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(MyRabbitDelayConstants.DEAD_LETTER_ROUTE_NAME_10);
    }

    // 声明死信队列B绑定关系
    @Bean
    public Binding deadLetterBindingB(@Qualifier("deadLetterQueueB") Queue queue, @Qualifier("deadLetterExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(MyRabbitDelayConstants.DEAD_LETTER_ROUTE_NAME_30);
    }

}
